﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Generator.DB
{
    [Serializable]
    public class Relation
    {
        public RelationTable PrimaryTable { get; set; }
        public RelationTable ForeignTable { get; set; }

        public override string ToString()
        {
            return string.Format("{0}-{1}", PrimaryTable.TableName, ForeignTable.TableName);
        }

        public string GetDiscription()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("PrimaryTable:{0}", PrimaryTable.TableName));
            sb.AppendLine(string.Format("PrimaryKey  :{0}", PrimaryTable.KeyField));
            sb.AppendLine(string.Format("DisplayField:{0}", PrimaryTable.ValueField));
            sb.AppendLine(string.Format("ForeignTable:{0}", ForeignTable.TableName));
            sb.AppendLine(string.Format("ForeignKey  :{0}", ForeignTable.KeyField));
            return sb.ToString();
        }

        public override int GetHashCode()
        {
            return string.Format("{0}{1}{2}{3}", PrimaryTable.TableName, PrimaryTable.KeyField, ForeignTable.TableName, ForeignTable.KeyField).GetHashCode();
        }

        public override bool Equals(object obj)
        {
            Relation re = obj as Relation;
            if (re == null) return false;
            return this.ForeignTable.Equals(re.ForeignTable) && this.PrimaryTable.Equals(re.PrimaryTable);
        }

    }

    [Serializable]
    public class RelationTable
    {
        public string TableName { get; set; }
        public string KeyField { get; set; }
        public string ValueField { get; set; }

        public override bool Equals(object obj)
        {
            RelationTable re = obj as RelationTable;
            if (re == null) return false;

            return this.TableName.Equals(re.TableName) &&
                    this.KeyField.Equals(re.KeyField);
                    //&&
                    //this.ValueField.Equals(re.ValueField);

        }

        public override int GetHashCode()
        {
            return string.Format("{0}{1}", TableName, KeyField).GetHashCode();
        }
    }

}
